<?php

namespace App\Model\Api;
use App\Model\BaseModel;
use App\Model\Room;

use Illuminate\Database\Eloquent\Model;

class TreasureBoxOpen extends BaseModel
{
    protected $connection = 'api';
    protected $table = 'treasure_box_open';
    private $fields = ['*'];

    public function addData($room_id, $is_open) {
        return curl_request(getKkaudioApiUrl() . '/inner/treasure_box/open/upsert', [
            'room_id' => $room_id,
            'is_open' => $is_open
        ], 'urlencoded', 'GET');
    }

    public function getList($limit, $page) {
        $query = new self();
        $query = $query->with(['room:room_id,name,room_uid']);
        return $query->orderBy('id','desc')->paginate($limit,$this->fields,'page',$page);
    }

    public function room() {
        return $this->belongsTo(Room::class, 'room_id', 'room_id')->withDefault([
            'room_uid' => '空'
        ]);
    }

    public function getProbab() {
        return curl_request(getKkaudioApiUrl() . '/inner/treasure_box/probability', [
        ], 'urlencoded', 'GET');
    }

    public function setProbab($value) {
        return curl_request(getKkaudioApiUrl() . '/inner/treasure_box/probability', [
            'value' => $value
        ], 'urlencoded', 'GET');
    }
}
